<template>
  <view class="my-login-container">
    <view class="content">
      <!-- 提示登录的图标 -->
      <uni-icons  type="contact-filled" size="100" color="#AFAFAF" ></uni-icons>
      <!-- 一键登录按钮 -->
      <button type="primary" class="login-btn" open-type="getUserInfo" @getuserinfo="getUserInfo">一键登录</button>
      <!-- 登录提示 -->
      <view class="tips-text">
        登录后享受更多权益
      </view>
    </view>
  </view>
</template>

<script>
  import {mapMutations} from 'vuex'
  export default {
    name:"my-login",
    data() {
      return {
        
      };
    },
    methods: {
      ...mapMutations('user', ['updateUserInfo', 'updateToken']),
      // 获取用于信息用于用户登录
      getUserInfo(e){
        console.log(e);
        // 判断是否获取用户成功
        if(e.detail.errMsg === 'getUserInfo:fail auth deny') return uni.$showMsg('您取消了授权')
        // 将用户信息存储到vuex中
        this.updateUserInfo(e.detail.userInfo)
        
        // 获取登录成功后的 Token 字符串
        this.getToken(e.detail)
      },
      async getToken(info){
        // 调用微信登录接口
        const [err, res] = await uni.login().catch(err=> err)
        console.log(res);
        // 判断是否调用 uni.login 失败
        if(err || res.errMsg !== 'login:ok') return uni.$showMsg('登录失败!')
        // 准备调用token接口的参数
        const query = {
          code: res.code,
          encryptedData: info.encryptedData,
          rawData: info.rawData,
          iv: info.iv,
          signature: info.signature
        }
        // 获取token
        const {data: loginResult} = await uni.$http.post('/users/wxlogin', query)
        console.log(loginResult);
        if(loginResult.meta.status !== 200) return uni.$showMsg('登录失败')
        this.updateToken(loginResult.message.token)
      }
    }
  }
</script>

<style lang="scss">
.my-login-container{
  background-color: #ffffff;
  height: 1334rpx;
  .content{
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    height: 750rpx;
    position: relative;
    overflow: hidden;
    background-color: #f8f8f8;
    
    &::after{
      content: ' ';
      display: block;
      position: absolute;
      left: 0;
      bottom: 0;
      width: 100%;
      height: 40px;
      background-color: #ffffff;
      border-radius: 100%;
      transform: translateY(50%);
    }
    .login-btn{
      background-color: #C00000;
      width: 90%;
      border-radius: 100px;
      margin: 15px 0;
    }
    .tips-text{
      font-size: 12px;
      color: gray;
    }
  }
}
</style>